<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
class ProductController extends CoreController {

	public function index(){
         if (I('get.p')) {
            $page=I('get.p');
        }else{
            $page = 1;
        }
        $ProductModel = M('Product');
        $List = $ProductModel->order('id desc')->page($page.',10')->select();
        $this->assign('list',$List);       
        $count = $ProductModel->count();
        $Page = new \Think\Page($count,10);
        $show = $Page->show();
        $this->assign('page',$show);

        $this->assign('addText','添加商品');
        $this->assign('addNew',U('add'));
        $this->assign('edit',U('add'));
        $this->assign('del',U('delUser'));
        $this->display();
    }

    public function add(){
            $id=I('get.id',0,'intval');
            $cateModel=D('Category');
            if($id>0){
                //查询产品
                $ProductModel = M('Product');
                $product = $ProductModel->find($id);
                //查询产品图片
                $pImageModel = M('product_image');
                $pImage = $pImageModel->where('product_id='.$id)->select();
                //组装图片数据
                $product['imgs'][]=$product['img'];
                foreach ($pImage as $key => $value) {
                    $product['imgs'][]=$value['url'];
                }
                //获取产品分类
                $pcModel = M('product_to_category');
                $ptc = $pcModel->where('product_id='.$id)->select();
                $categories = array();
                foreach ($ptc as $value) {
                    $categories[]=$value['category_id'];
                }
                if(!empty($categories)){
                    $optionStr = $cateModel->getCateOptionHtml($categories);
                }else{
                    $optionStr = $cateModel->getCateOptionHtml(); 
                }

                //模板变量赋值
                $this->assign('optionStr',$optionStr);
                $this->assign('product',$product);
                $this->assign('id',$id);
            }else{
                $this->optionStr = $cateModel->getCateOptionHtml();
            }
	        $this->assign('action',U('save'));
	        $this->assign('cancel',U('index'));
            $this->assign('upload',U('upload'));

	        //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
            $this->setStaticFile('plugin/webupload/webuploader.css','head','css');
            $this->setStaticFile('plugin/webupload/webuploader.js','head','js');
            $this->setStaticFile('plugin/ueditor/ueditor.config.js','head','js');
            $this->setStaticFile('plugin/ueditor/ueditor.all.js','head','js');
	        $this->display();
        
    }

    public function save(){
    	if(IS_POST){
    		$id = I('post.id',0,'intval');
            
            $image = I('post.productImage');
    		$data = array(
    			'name' => I('post.name'),
                'keyword' => I('post.keyword'),
                'description' => I('post.description'),
                'sku' => I('post.sku'),
                'stock' => I('post.stock',0,'intval'),
    			'price' => I('post.price'),
                'img' => $image[0],
                'content' => I('post.content'),
                'status'=>I('post.status',0,'intval'),
    			);
            $productModel = D('Product');
            
            if($productModel->create($data)){
                if($id>0){
                    $productModel->where('id='.$id)->save();
                    $pid=$id;
                }else{
                    $pid=$productModel->add();
                }
                
                if($pid>0){
                    //保持商品图片
                    $pImageModel = M('product_image');
                    $pImageModel->where('product_id='.$pid)->delete();
                    $data=array();
                    foreach ($image as $key => $img) {
                        if($key>0){
                           $data[]=array(
                            'product_id'=>$pid,
                            'title'=>'',
                            'url'=>$img
                           ); 
                        }
                    }
                    
                    $pImageModel->addAll($data);

                    //保存商品分类
                    $pcModel = M('product_to_category');
                    $pcModel->where('product_id='.$pid)->delete();
                    $data = array();
                    foreach (I('post.category') as $key => $cid) {
                        $data[]=array(
                            'product_id'=>$pid,
                            'category_id'=>$cid,
                           ); 
                    }
                    $pcModel->addAll($data);

                    //返回结果
                    $backdata['status']='OK';
                    $backdata['msg']='保持成功';
                    $backdata['url']=U('index');
                    $this->ajaxReturn($backdata);
                }else{
                    $backdata['status']='error';
                    $backdata['msg']=$productModel->getError();
                    $this->ajaxReturn($backdata);
                }
            }else{
                $backdata['status']='error';
                $backdata['msg']=$productModel->getError();
                $this->ajaxReturn($backdata);
            }
    	}else{

        } 
    }

    public function upload(){
        $upload = new \Think\Upload();
        $upload->maxSize=3145728;
        $upload->exts=array('jpg', 'gif', 'png', 'jpeg');
        $upload->savePath = 'product/';
        $info=$upload->uploadOne($_FILES['file']);
        if(!$info) {// 上传错误提示错误信息
            $data['status']='error';
            $data['msg']=$upload->getError();
            $this->ajaxReturn($data);
        }else{// 上传成功 获取上传文件信息
            $data['status']='OK';
            $data['msg']='上传成功';
            $data['file']=$info['savepath'].$info['savename'];
            $this->ajaxReturn($data);
        }
    }
}